/**
* Created by ren on 2017/1/4.
*/
<template>
  <div class='recharge'>
    <div style="margin-top: 51px;margin-left: 28px;font-size: 20px;color: #666666;margin-bottom: 75px">余额充值</div>
    <div>
      <input type="text" v-model="num" placeholder="请输入充值金额"
             style="margin-left:100px;margin-right:16px;width: 600px;height: 54px;background: #f5f5f5;border: 0;text-align: center"><span>元</span>
    </div>
    <div style="text-align: center;">
      <button class="btn"
              style="width: 170px;height: 49px;color: white;background: #f48315;margin-top: 125px;font-size: 20px"
              @click="addAccount">确定充值
      </button>
    </div>
    <Notify :parentThis='this'></Notify>
    <div id="yinlian"></div>
  </div>
</template>

<script>
  import API from '../../api'
  import Notify from '../Notify'
  export default {
    name: 'recharge',
    components: {
      Notify
    },
    data: function () {
      return {
        num: '',
        notifyStatus: 2,
        yinlian: ''
      }
    },
    methods: {
      addAccount: function () {
        this.butText = '充值中……'
        this.$http.post(API.addAccount, {
          userId: this.$cookie.get('id'),
          money: this.num + '00',
          useRange: 2
        }).then((ret) => {
          var result = ret.body
          if (typeof result === 'object') {
            var reader = new window.FileReader()
            reader.addEventListener('loadend', function () {
              result = reader.result
              /**
               * 银联支付
               * @type {Element}
               */
              var cont = document.getElementById('yinlian')
              cont.innerHTML = result
              var oldScript = cont.getElementsByTagName('script')[0]
              cont.removeChild(oldScript)
              var newScript = document.createElement('script')
              newScript.type = 'text/javascript'
              newScript.innerHTML = oldScript.innerHTML
              cont.appendChild(newScript)
            })
            reader.readAsText(result)
          } else {
            /**
             * 银联支付
             * @type {Element}
             */
            var cont = document.getElementById('yinlian')
            cont.innerHTML = result
            var oldScript = cont.getElementsByTagName('script')[0]
            cont.removeChild(oldScript)
            var newScript = document.createElement('script')
            newScript.type = 'text/javascript'
            newScript.innerHTML = oldScript.innerHTML
            cont.appendChild(newScript)
          }
        }, (err) => {
          console.log(err)
        })
      }
    },
    watch: {
      num: function (newVal, oldVal) {
        if (!/^[1-9]*[1-9][0-9]*$/.test(newVal) && newVal !== '') {
          this.num = oldVal
        }
      }
    }
  }
</script>